package com.teaching.management.controller;

import com.teaching.management.entity.User;
import com.teaching.management.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import jakarta.servlet.http.HttpSession;
import java.util.Map;

/**
 * 管理员控制器
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 显示管理员仪表盘
     */
    @GetMapping("/dashboard")
    public String showDashboard(Model model, HttpSession session) {
        // 检查用户是否登录
        User user = (User) session.getAttribute("user");
        if (user == null || !"管理员".equals(user.getRole())) {
            return "/login";
        }

        // 获取系统概览数据
        Map<String, Long> overview = adminService.getSystemOverview();
        model.addAttribute("overview", overview);

        return "admin/dashboard";
    }

    /**
     * 教师管理页面
     */
    @GetMapping("/teachers")
    public String showTeacherManagement(HttpSession session) {
        // 检查用户是否登录
        User user = (User) session.getAttribute("user");
        if (user == null || !"管理员".equals(user.getRole())) {
            return "/login";
        }

        return "admin/teachers";
    }

    /**
     * 学生管理页面
     */
    @GetMapping("/students")
    public String showStudentManagement(HttpSession session) {
        // 检查用户是否登录
        User user = (User) session.getAttribute("user");
        if (user == null || !"管理员".equals(user.getRole())) {
            return "/login";
        }

        return "admin/students";
    }

    /**
     * 课程管理页面
     */
    @GetMapping("/courses")
    public String showCourseManagement(HttpSession session) {
        // 检查用户是否登录
        User user = (User) session.getAttribute("user");
        if (user == null || !"管理员".equals(user.getRole())) {
            return "/login";
        }

        return "admin/courses";
    }

    /**
     * 教室管理页面
     */
    @GetMapping("/classrooms")
    public String showClassroomManagement(HttpSession session) {
        // 检查用户是否登录
        User user = (User) session.getAttribute("user");
        if (user == null || !"管理员".equals(user.getRole())) {
            return "/login";
        }

        return "admin/classrooms";
    }

    /**
     * 选课管理页面
     */
    @GetMapping("/course-selections")
    public String showCourseSelectionManagement(HttpSession session) {
        // 检查用户是否登录
        User user = (User) session.getAttribute("user");
        if (user == null || !"管理员".equals(user.getRole())) {
            return "/login";
        }

        return "admin/course-selections";
    }

    /**
     * 成绩管理页面
     */
    @GetMapping("/grades")
    public String showGradeManagement(HttpSession session) {
        // 检查用户是否登录
        User user = (User) session.getAttribute("user");
        if (user == null || !"管理员".equals(user.getRole())) {
            return "/login";
        }

        return "admin/grades";
    }

    /**
     * 管理员个人信息页面
     */
    @GetMapping("/profile")
    public String showAdminProfile(HttpSession session) {
        // 检查用户是否登录
        User user = (User) session.getAttribute("user");
        if (user == null || !"管理员".equals(user.getRole())) {
            return "/login";
        }

        return "admin/profile";
    }
}